/*
 * @Author: your name
 * @Date: 2020-09-15 16:19:30
 * @LastEditTime: 2020-09-21 16:04:37
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tower_defense_demo\assets\scripts\home_scene.ts
 */

import { SoundMgr } from "./sound_mgr"
import { UG } from "./modules/ugame"
import LoadingDoor from "./loading_door"

const { ccclass, property } = cc._decorator;

@ccclass
export default class HomeScene extends cc.Component {

    // ================动画=====================
    @property(LoadingDoor)
    public loading_door: LoadingDoor = null;
    @property(cc.Animation)
    public start_anim: cc.Animation = null;
    @property(cc.Animation)
    public user_game_info_anim: cc.Animation = null;

    // ================用户节点==================
    @property(cc.Node)
    public user1_node: cc.Node = null;
    @property(cc.Node)
    public user2_node: cc.Node = null;
    @property(cc.Node)
    public user3_node: cc.Node = null;

    public game_started: boolean = false;
    public out_side_scene: boolean = false;

    start() {
        // 显示用户信息
        this.show_user_data();

        // 延迟开门
        this.scheduleOnce(()=>{
            this.start_anim.play("home_scene_start_anim");
        }, 0.5)
    }

    onLoad() {
        this.game_started = false;
        this.out_side_scene = false;
        // 播放背景音乐
        SoundMgr.play_music("sounds/music/home_scene_bg", true);
    }

    // 设置现实用户星数
    public set_star_score(user_node: cc.Node, user): void {
        let star_score_label: cc.Label = user_node.getChildByName("star_score").getComponent(cc.Label);
        star_score_label.string = user.star_num + "/" + user.star_total;
    }

    // 展示用户信息
    public show_user_data(): void {
        this.set_star_score(this.user1_node, UG.user_data[0]);
        this.set_star_score(this.user2_node, UG.user_data[1]);
        this.set_star_score(this.user3_node, UG.user_data[2]);
    }

    // ==========================点击事件===================================
    public start_game(): void {
        if (this.game_started) {    // 开始游戏按钮只调用一次
            return;
        }
        this.game_started = true;
        SoundMgr.play_effect("sounds/click");

        // 收起动画
        this.start_anim.play("start_button_click_anim");

        // 收起动画结束，播放进场动画
        this.scheduleOnce(()=>{
            this.user_game_info_anim.play("uinfo_enter_anim");
        }, this.start_anim.currentClip.duration);
    }

    // 关闭用户信息框
    public close_uinfo_dlg(): void {
        // 用户信息框隐藏动画
        this.user_game_info_anim.play("reverse_uinfo_enter_anim");

        // start按钮隐藏动画动画
        this.scheduleOnce(()=>{
            this.game_started = false;
            this.start_anim.play("reverse_start_button_click_anim");
        }, this.user_game_info_anim.currentClip.duration)
    }

    // 关门 加载
    public close_door(): void {
        this.loading_door.set_door_state(0);     // 0关门，1开门
        this.scheduleOnce(()=>{
            // 开门
            this.loading_door.open_the_door(null);
        }, 0.5);
    }

    // 跳转到about_scene
    public goto_about_scene(): void {
        if (this.out_side_scene) {  // 防止多次跳转
            return;
        }

        this.out_side_scene = true;
        SoundMgr.play_effect("sounds/click");

        // 跳转
        this.loading_door.close_the_door(()=>{
            this.scheduleOnce(()=>{
                cc.director.loadScene("about_scene");
            }, 0.5);
        });
    }

    // 判断使用哪个用户进入游戏
    public on_user_entry_click(e, c): void {
        if (this.out_side_scene) {  // 防止多次响应
            return;
        }
        this.out_side_scene = true;

        let user_index: number = parseInt(c);
        UG.set_cur_user(user_index);
        SoundMgr.play_effect("sounds/click");

        this.loading_door.close_the_door(()=>{
            this.scheduleOnce(()=>{
                cc.director.loadScene("roadmap_scene");
            }, 0.5);
        });
    }

}
